package com.my.note.service;

import com.my.note.mapper.ResourceMapper;
import com.my.note.mapper.RoleMapper;
import com.my.note.mapper.UserMapper;
import com.my.note.pojo.ResourceData;
import com.my.note.pojo.RoleData;
import com.my.note.pojo.RoleResourceData;
import com.my.note.pojo.UserData;
import com.my.note.pojo.UserRoleData;
import com.my.note.vo.RoleVO;
import com.my.note.vo.UserVO;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lilun <a href="mailto:lil@chsi.com.cn">lilun</a>
 * @version $Id$ 2019年03月21 9:19
 */
@Component
public class UserService {

    @Autowired
    ResourceMapper resourceMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    SqlSessionFactory sqlSessionFactory;

    public ResourceData getResourceByUrl(String url) {
        return resourceMapper.getResourceByUrl(url);
    }

    public ResourceData getResourceById(String Id) {
        return resourceMapper.getResourceById(Id);
    }

    public List<ResourceData> getResourceList() {
        return resourceMapper.getResouceList();
    }

    public List<RoleData> getRolesByResourceId(String resourceId) {
        return roleMapper.getRoleResourceListByResId(resourceId).stream().map(
                roleResourceData -> roleMapper.getRoleById(roleResourceData.getRoleId())).collect(
                Collectors.toList());
    }

    public UserData getUserByUserName(String userName) {
        return userMapper.getUserByUserName(userName);
    }

    public UserData getUserById(String id) {
        return userMapper.getUserById(id);
    }

    public RoleData getRoleById(String id) {
        return roleMapper.getRoleById(id);
    }

    public RoleData getRoleByRoleName(String roleName) {
        return roleMapper.getRoleByRoleName(roleName);
    }

    public List<UserRoleData> getUserRoleByUserId(String userId) {
        return userMapper.getUserRoleByUserId(userId);
    }

    public List<RoleResourceData> getRoleResourceByRoleId(String roleId) {
        return roleMapper.getRoleResourceListByRoleId(roleId);
    }

    public List<RoleData> getRolesByUserId(String userId) {
        return userMapper.getUserRoleByUserId(userId).stream().map(
                userRoleData -> roleMapper.getRoleById(userRoleData.getRoleId())).collect(
                Collectors.toList());
    }

    public List<ResourceData> getResourceListByRoleId(String roleId) {
        return roleMapper.getRoleResourceListByRoleId(roleId).stream().map(
                roleResourceData -> resourceMapper.getResourceById(roleResourceData.getResourceId())).collect(
                Collectors.toList());
    }

    public List<RoleData> getRoleList() {
        return roleMapper.getRoleList();
    }

    public List<RoleVO> getRoleVOList() {
        return roleMapper.getRoleList().stream().map(role -> {
            RoleVO vo = new RoleVO();
            vo.setId(role.getId());
            vo.setRoleName(role.getRoleName());
            vo.setMark(role.getMark());
            vo.setUrls(String.join("、", roleMapper.getRoleResourceListByRoleId(role.getId()).stream().map(
                    roleResourceData -> resourceMapper.getResourceById(roleResourceData.getResourceId()).getUrl()).collect(
                    Collectors.toList())));
            return vo;
        }).collect(Collectors.toList());
    }

    public List<UserVO> getUserVOList() {
        return userMapper.getUserList().stream().map(user -> {
            UserVO vo = new UserVO();
            vo.setId(user.getId());
            vo.setUserName(user.getUserName());
            vo.setRoles(String.join("、",
                    getRolesByUserId(user.getId()).stream().map(p -> p.getRoleName()).collect(Collectors.toList())));
            return vo;
        }).collect(Collectors.toList());
    }

    public void save(UserData userData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.saveUser(userData.getId(), userData.getUserName(), userData.getPassword());
        session.commit(true);
        session.close();
    }

    public void save(RoleData roleData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.saveRole(roleData.getId(), roleData.getRoleName(), roleData.getMark());
        session.commit(true);
        session.close();
    }

    public void save(ResourceData resourceData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        ResourceMapper mapper = session.getMapper(ResourceMapper.class);
        mapper.saveResource(resourceData.getId(), resourceData.getUrl(), resourceData.getMark());
        session.commit(true);
        session.close();
    }

    public void save(UserRoleData userRoleData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.saveUserRole(userRoleData.getId(), userRoleData.getRoleId(), userRoleData.getUserId());
        session.commit(true);
        session.close();
    }

    public void save(RoleResourceData roleResourceData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.saveRoleResource(roleResourceData.getId(), roleResourceData.getRoleId(), roleResourceData.getResourceId());
        session.commit(true);
        session.close();
    }

    public void delete(UserRoleData userRoleData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.deleteUserRole(userRoleData.getId());
        session.commit(true);
        session.close();
    }

    public void delete(RoleResourceData roleResourceData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.deleteRoleResource(roleResourceData.getId());
        session.commit(true);
        session.close();
    }

    public void delete(UserData userData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.deleteUser(userData.getId());
        session.commit(true);
        session.close();
    }

    public void delete(RoleData roleData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.deleteRole(roleData.getId());
        session.commit(true);
        session.close();
    }

    public void delete(ResourceData resourceData) {
        SqlSession session = sqlSessionFactory.openSession(true);
        ResourceMapper mapper = session.getMapper(ResourceMapper.class);
        mapper.deleteResource(resourceData.getId());
        session.commit(true);
        session.close();
    }
}
